home *** CD-ROM | disk | FTP | other *** search
/ Top 200 Programs / Top 200 Programs.iso / Bob8 / THOMPSON / LIBERTY / PRODUCT / TUTORIAL.EXE / WK4HW1.TXT < prev    next >
Text File  |  1996-03-05  |  12KB  |  356 lines

  1. Week 4 Homework
  2. Liberty BASIC programming course
  3. Copyright 1996 Shoptalk Systems
  4. All Rights Reserved
  5.  
  6.  
  7. Using NOMAINWIN
  8. ==============================================================================
  9.  
  10. The default text window that opens with each Liberty BASIC program we create 
  11. can be optionally turned off.  This can be desirable when we don't want our 
  12. application to be operated out of a text window.  The statement that 
  13. accomplishes this effect is NOMAINWIN.  Simply include NOMAINWIN in your 
  14. program's source code anywhere at all.  This accomplishes two things:
  15.  
  16. 1) It hides the program's main window;
  17.  
  18. 2) It prevents the 'Execution Complete' notice from appearing at the end
  19. of a program run.
  20.  
  21. This comes at a cost.  Your program must close itself down properly when all 
  22. the windows have been closed.  Below is a modified version of our WK4PROG6.BAS
  23. program.  In this case all we needed to do was add the NOMAINWIN statement.  
  24. No other changes are needed because we have an END statement after our example
  25. window and because we don't use the program's main window for anything.
  26.  
  27.  
  28.     'WK4PROG7.BAS - (WK4PROG6.BAS modified)
  29.     'open a window with a button, a textbox, and a statictext
  30.     'show how trapclose works
  31.     'the window is sized at 300 by 100 at position 200, 150
  32.  
  33.     nomainwin  'do not open a main window
  34.  
  35.     WindowWidth = 300
  36.     WindowHeight = 100
  37.     UpperLeftX = 200
  38.     UpperLeftY = 150
  39.     button #myFirst.ok, "OK!", [okClicked], UL, 220, 35
  40.     textbox #myFirst.field, 10, 35, 200, 25
  41.     statictext #myFirst.label, "Type some text here.", 10, 10, 150, 25
  42.     open "myname's first window!" for window as #myFirst
  43.  
  44.     'send the trapclose command
  45.     print #myFirst, "trapclose [quit]"
  46.  
  47. [waitHere]  'now stop and wait
  48.     input aVar$
  49.     goto [quit]
  50.  
  51. [okClicked]  'OK! was clicked.  Get the contents of the entry field
  52.  
  53.     'print a command to the textbox
  54.     print #myFirst.field, "!contents?"
  55.     'now get the contents from the textbox
  56.     input #myFirst.field, aString$
  57.  
  58.     'now pop up a notice saying what was in the textbox
  59.     notice aString$
  60.  
  61. [quit]
  62.     'ask if the user wants to quit
  63.     confirm "Really Quit?"; answer$
  64.     if answer$ <> "yes" then [waitHere] 'abort quitting
  65.  
  66.     'now close the window
  67.     close #myFirst
  68.  
  69.     end
  70.  
  71.  
  72. If there was a GOTO [waitHere] statement after the CLOSE #myFirst statement 
  73. in our program above, then our program would not terminate properly, and 
  74. there would be no error message to clue us in.
  75.  
  76.  
  77. The Structure of a Liberty BASIC GUI Program
  78. ==============================================================================
  79.  
  80. We've taken a look at some simple elements of Liberty BASIC GUI programming.
  81. Now let's consider how to organize code in a logical and practical way when
  82. creating GUI programs in LB.
  83.  
  84.  
  85. Organization
  86. ==============================================================================
  87.  
  88. In the case of a project that uses more than one window, we should make sure
  89. that code for each window is grouped together.  Most applications are designed
  90. around a cental window that is always open and from where other windows are 
  91. launched.  When the central window is closed, the application ends.  Here is 
  92. a simple outline (not a working program) that demonstrates this idea:
  93.  
  94.  
  95.     'MYPROG.BAS
  96.     'This is an example outline
  97.     'A description of the program should go here
  98.  
  99.     '-----------------------Let's define our main application window here
  100.     button #main.1, ....
  101.     button #main.2, ....
  102.     textbox ....
  103.     open "My Application" for window as #main
  104.     print #main, "trapclose [closeMain]"
  105.  
  106.  
  107. [waitWinMain]  'wait here for user action
  108.     input aVar$
  109.     goto [waitWinMain]
  110.  
  111.  
  112. [button1Clicked]  'here is a routine that handles when a button is clicked
  113.     'do something here
  114.     'now go back and wait
  115.     goto [waitWinMain]
  116.  
  117. [button2Clicked]  'here is a routine that handles when a button is clicked
  118.     'do something here
  119.     'now go back and wait
  120.     goto [waitWinMain]
  121.  
  122.  
  123. [closeMain]  'perform some cleanup code and close the window
  124.     close #main
  125.     gosub [sub1]  'act on user action
  126.  
  127.     'end application
  128.     end
  129.  
  130.  
  131.  
  132. [openWin2]  '--------------------------Let's define our second window here
  133.  
  134.     button #2.3, ....
  135.     button #2.4, ....
  136.     textbox ....
  137.     open "Window 2" for window as #2
  138.     print #2, "trapclose [close2]"
  139.  
  140.  
  141. [waitWin2]  'wait here for user action
  142.     input aVar$
  143.     goto [waitWin2]
  144.  
  145.  
  146. [button3Clicked]  'here is a routine that handles when a button is clicked
  147.     'do something here
  148.     'now go back and wait
  149.     goto [waitWin2]
  150.  
  151.  
  152. [button4Clicked]  'here is a routine that handles when a button is clicked
  153.     'do something here
  154.     'now go back and wait
  155.     goto [waitWin2]
  156.  
  157.  
  158. [close2]  'perform some cleanup code and close the window
  159.     close #2
  160.     gosub [sub2]  'act on user action
  161.     goto [waitWin2]
  162.  
  163.  
  164. '--------------------------------------------------------------------------
  165.  
  166.     'We might place code here containing general subroutines
  167.  
  168.  
  169. See how we arrange the code for each window like so:
  170.  
  171. [openMainWindow]
  172. [userAction1]
  173. [userAction2]
  174. [closeMainWindow]
  175.     end
  176.  
  177. [openWindow1]
  178. [userAction3]
  179. [userAction4]
  180. [closeWindow1]
  181.  
  182. [openWindow2]
  183. [userAction5]
  184. [userAction6]
  185. [closeWindow2]
  186.  
  187. The names we give to our branch labels above are of course generic.  All the
  188. code for a given window and user actions performed on it should be grouped
  189. together.  By structuring things in this way it is relatively easy to develop
  190. and debug your programs.
  191.  
  192. NOTE: Be careful to avoid creating a program that closes all its windows but
  193. doesn't actually end.  If the program stops at an input statement after
  194. closing all of it's windows, it will not terminate properly and resources
  195. will not be freed.  If the program loops continuously after closing all its
  196. windows then our WINDOWS message handler (the one set up to Liberty BASIC)
  197. will be locked out.  In this case press CTRL-Break to halt the errant
  198. program.
  199.  
  200.  
  201. Using Dialog Boxes
  202. ==============================================================================
  203.  
  204. A dialog box is a special kind of window.  They are different in several
  205. respects from other windows:
  206.  
  207. 1) Pressing the Tab key causes WINDOWS to cycle through the controls added
  208. to a dialog box.  Each control is made the active one in turn.
  209.  
  210. 2) Dialog boxes can be made modal.  This means that if I have a first window
  211. open and then I open a modal dialog box, that I will be unable to select the
  212. first window until the modal dialog is closed.  Most 'About' boxes are modal
  213. dialogs, as are many 'Properties' type windows.  For an example, select
  214. Liberty BASIC's Help menu and click on 'About Liberty BASIC'.  You won't be
  215. able to do anything else with Liberty BASIC until you close the dialog box
  216. that appears.
  217.  
  218. 3) In Liberty BASIC, dialogs ignore the UpperLeftX and UpperLeftY variables,
  219. so a dialog box's initial screen position cannot be determined in this way.
  220.  
  221. 4) Dialog boxes cannot be resized by the user.
  222.  
  223. Some applications are appropriately constructed completely of dialog boxes
  224. and have no other kind of window at all.  Take a look at the included program
  225. FreeForm (FF12.BAS).  This program is more typical.  The main application
  226. windows is a graphics type, but most of the other windows are dialog boxes.
  227.  
  228.  
  229. 'Canned' Dialog Boxes
  230. ==============================================================================
  231.  
  232. There are some simple preconstructed dialog boxes in Liberty BASIC.  These are
  233. useful for displaying simple notices, asking for a yes and no response,
  234. prompting the user to type some string, or selecting a filename from a list of
  235. filenames.
  236.  
  237. Here are some examples:
  238.  
  239.  
  240.     'display a notice
  241.     notice "File Not Found!"
  242.  
  243.     'ask a yes or no question
  244.     confirm "Would you like to skip the tutorial?"; answer$
  245.  
  246.     'prompt for input string
  247.     prompt "Please enter your name:"; name$
  248.  
  249.     'get a filename from the user
  250.     filedialog "Pick a text file", "*.txt", selection$
  251.  
  252.  
  253. There are right and wrong times to use these kinds of dialogs.  Here is the
  254. HILO.BAS program written using only the above dialog boxes.  This illustrates
  255. a wrong way to write a WINDOWS program.
  256.  
  257.  
  258.     ' WK4PROG8.BAS
  259.     ' Here is an interactive HI-LO
  260.     ' Program
  261.  
  262.     'don't use a main window
  263.     nomainwin
  264.  
  265. [start]
  266.     guessMe = int(rnd(1)*100) + 1
  267.  
  268.     ' Clear the screen and print the title and instructions
  269.     notice "HI-LO" + chr$(13) + _
  270.      "I have decided on a number between one " + _
  271.      "and a hundred, and I want you to guess " + _
  272.      "what it is.  I will tell you to guess " + _
  273.      "higher or lower, and we'll count up " + _
  274.      "the number of guesses you use."
  275.  
  276. [ask]
  277.     ' Ask the user to guess the number and tally the guess
  278.     prompt "OK.  What is your guess ?"; guess$
  279.     guess = val(guess$)
  280.  
  281.     ' Now add one to the count variable to count the guesses
  282.     let count = count + 1
  283.  
  284.     ' check to see if the guess is right
  285.     if guess = guessMe then goto [win]
  286.     ' check to see if the guess is too low
  287.     if guess < guessMe then notice "Guess higher."
  288.     ' check to see if the guess is too high
  289.     if guess > guessMe then notice "Guess lower."
  290.  
  291.     ' go back and ask again
  292.     goto [ask]
  293.  
  294. [win]
  295.     ' beep once and tell how many guesses it took to win
  296.     beep
  297.     notice "You win!  It took" + str$(count) + "guesses."
  298.  
  299.     ' reset the count variable to zero for the next game
  300.     let count = 0
  301.  
  302.     ' ask to play again
  303.     confirm "Play again (Y/N)?"; play$
  304.     if instr("YESyes", play$) > 0 then goto [start]
  305.  
  306.     end
  307.  
  308.  
  309. What is wrong with the program above?  Technically there is nothing wrong
  310. with it because it works.  However one obvious thing wrong is that there's
  311. only one place where you can exit the program (at the end of each game).
  312. It also looks sloppy.  It doesn't have one application window that is open
  313. all the time as most WINDOWS programs do, so it will seem strange to most
  314. users.
  315.  
  316.  
  317. More About Statictext
  318. ==============================================================================
  319.  
  320. We saw in our earlier installment of this Week 4 lesson how a statictext
  321. control is used to label other controls in a window.  Statictext controls do
  322. not have to always display the same label.  We can print to them in similar
  323. fashion to the way we print to a textbox control.  Try the example code below:
  324.  
  325.  
  326.     'WK4PROG9.BAS
  327.     'demonstrate printing to statictext control
  328.  
  329.     statictext #countdown.stext, "Counting down:", 10, 10, 200, 25
  330.     open "Countdown" for window as #countdown
  331.  
  332.     for count = 10 to 1 step -1
  333.         print #countdown.stext, "Counting down: "; count
  334.         t$ = time$()
  335.         while t$ = time$()
  336.         wend
  337.     next count
  338.  
  339.     print #countdown.stext, "Counting Down: Done."
  340.  
  341.     end
  342.  
  343.  
  344. Homework Assignment
  345. ==============================================================================
  346.  
  347. Create a version of the WK4PROG8.BAS program above (call it MYHILO.BAS).  The
  348. program should have its own application window using the dialog window type.
  349. This window should have at least one statictext control.  One of these should
  350. indicate whether to guess higher or lower and to count the guesses.  There
  351. should be a textbox for entering guesses, and a button labeled guess to click
  352. on to register each guess.  An additional button should be labeled 'About'.
  353. This button should cause an About box to be displayed for the game.  The About
  354. box can be a canned dialog box, or you can construct one yourself using a
  355. modal dialog box.
  356.